<template>
  <div class="login">
    <div class="contanier">
      <el-form  :rules="loginRules" ref="loginFormRef" :model="loginForm"  class="loginForm">
        <el-form-item prop="username" >
          <el-input prefix-icon="iconfont icon-yonghuming" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-mima" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btn">
         <el-button type="primary" @click='login'>登录</el-button>
         <el-button type="info" @click='preSet'>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
    import {requestLoginData} from '@/network/login.js'
    export default {
        name: 'login',
        data() {
            return {
                    loginForm:{
                        username: 'admin',
                        password: '123456'
                    },
                    //验证规则
                    loginRules:{
                      username:[
                        { required: true, message: '请输入用户名称', trigger: 'blur' },
                        { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                      ],
                      password:[
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                      ]
                    }
                }
            },
        created() {
            
        },
        methods: {
          //发送登录的网络请求
          login(){
            this.$refs.loginFormRef.validate(async valid =>{
              if(!valid) return 
              const {data} = await requestLoginData({params:this.loginForm})
              if(data.meta.status === 200){
                this.$message.success('登录成功')
                window.sessionStorage.setItem('token',data.data.token)
                this.$router.push('/home')
              }else{
                this.$message.error('您输入的用户或者密码不正确')
              }
            })
          },
          //重置按钮的方法
          preSet(){
            this.$refs.loginFormRef.resetFields()
          }

        },
    }
</script>

<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100%;
  width: 100%;
  position: relative;
  .contanier {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    // padding: 20px;
    width: 500px;
    height: 350px;
    background-color: #fff;
    .loginForm{
      margin-top: 40px;
      width: 100%;
      padding: 10px;
      box-sizing: border-box;
      .btn{
        text-align: right;
      }
    }
  }
}
</style>
